package com.itcm.springsecurityoauth2demo.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * @author jinchengming
 * @version 1.0.0
 * @date 2022/10/21 11:01
 */

@RestController
@RequestMapping("/user")
public class UserController {


    /**
     * 获取当前用户
     */
    @RequestMapping("/getCurrentUser")
    public Object getCurrentUser(Authentication authentication){
        return authentication.getPrincipal();
    }

    @RequestMapping("/getJwtData")
    public Object getJwtData(HttpServletRequest request){
        String authorization = request.getHeader("Authorization");
        // Bearer ey......
        String token = authorization.substring(7);

        return Jwts.parser()
                .setSigningKey("hello_jwt".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token).getBody();
    }
}
